package com.gjy.design.observer.disruptor;

import java.util.function.Consumer;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-11-08 09:14:47
 */
public class DefaultSubscriber implements Subscriber {

    private final String id;
    private final Consumer<MessageEvent> messageHandler;
    private final Consumer<Throwable> errorHandler;

    public DefaultSubscriber(String id, Consumer<MessageEvent> messageHandler) {
        this(id, messageHandler, null);
    }

    public DefaultSubscriber(String id, Consumer<MessageEvent> messageHandler, Consumer<Throwable> errorHandler) {
        this.id = id;
        this.messageHandler = messageHandler;
        this.errorHandler = errorHandler;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public void onMessage(MessageEvent event) {
        if (messageHandler != null) {
            messageHandler.accept(event);
        }
    }

    @Override
    public void onError(Throwable throwable) {
        if (errorHandler != null) {
            errorHandler.accept(throwable);
        }
    }

}
